<template>
  <div>
    <section class="filter">
    <div class="filter-container">
      <el-form ref="form" :inline="true">
        <el-form-item label="敏感词：">
          <el-input
            v-model="listQuery.name"
            placeholder="请输入敏感词"
            class="filter-item"
            clearable
            @input="resetPage"
          />
        </el-form-item>
      </el-form>
    </div>
    <el-button type="success" icon="el-icon-circle-plus-outline" @click="createSensitive">新建</el-button>
  </section>

    <section class="result">
      <el-table
        :data="list"
        :header-cell-style="{textAlign: 'center', fontWeight: '600'}"
        :cell-style="{textAlign: 'center'}"
        highlight-current-row
      >
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="敏感词">
          <template slot-scope="scope">
            <span>{{ scope.row.sensitives }}</span>
          </template>
        </el-table-column>
        <!-- TODO: 后台缺少字段 -->
        <!-- <el-table-column label="权重">
          <template slot-scope="scope">
            <span></span>
          </template>
        </el-table-column>
        <el-table-column label="拦截次数">
          <template slot-scope="scope">
            <span></span>
          </template>
        </el-table-column>-->
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateTimeFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="el-button--success-text"
              @click="editSensitive(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              class="el-button--danger-text"
              @click="delSensitive(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </section>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <sensitive-create-dialog :dialogVisible.sync="createDialogVisible" @refreshList="getList" />

    <sensitive-edit-dialog :dialogVisible.sync="editDialogVisible" :sensitive="sensitive" @refreshList="getList" />
  </div>
</template>

<script>
import Pagination from '@/components/pagination'
import SensitiveCreateDialog from './components/sensitive-create-dialog'
import SensitiveEditDialog from './components/sensitive-edit-dialog'
import { loadList, deleteData } from '@/api/sensitive'

export default {
  name: 'SensitiveIndex',
  components: { Pagination, SensitiveCreateDialog, SensitiveEditDialog },
  data () {
    return {
      // 列表相关
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        name: undefined,
        page: 1,
        size: 10
      },
      sensitive: {},
      // 对话框相关
      createDialogVisible: false,
      editDialogVisible: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      // el-input和el-select执行clear后值为''，主动转换为undefined
      if (this.listQuery.name === '') {
        this.listQuery.name = undefined
      }

      const res = await loadList(this.listQuery)
      if (res.code === 200) {
        this.list = res.data
        this.total = res.total
      } else {
        this.$message({ type: 'error', message: res.errorMessage })
      }
    },
    // 重置分页page
    resetPage () {
      this.listQuery.page = 1
      this.getList()
    },
    // 新增数据
    createSensitive: function () {
      this.createDialogVisible = true
    },
    // 编辑数据
    editSensitive: function (sensitive) {
      this.editDialogVisible = true
      this.sensitive = sensitive
    },
    async delSensitive (id) {
      try {
        await this.showDeleteConfirm()
        this.doDelete(id)
      } catch (err) {
        this.$message({ type: 'info', message: '已取消删除' })
      }
    },
    // 删除确认
    // TODO: 按钮hover颜色
    async showDeleteConfirm () {
      return await this.$confirm('删除后敏感词将不可找回，确认将敏感词删除吗？', '删除', {
        confirmButtonText: '删除',
        confirmButtonClass: 'el-button--danger',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--warning'
      })
    },
    async doDelete (id) {
      const res = await deleteData(id)
      if (res.code === 0) {
        this.submitSuccess()
        this.$message({ type: 'success', message: '操作成功！' })
      } else {
        this.$message({ type: 'error', message: res.errorMessage })
      }
    }

  }
}
</script>
